/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.AcademicYearDAL;
import mnstudent.datatranferobject.AcademicYearDTO;
import mnstudent.datatranferobject.AcademicYearDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author trungpv
 */
public class AcademicYearBLL {
    
    AcademicYearDAL academicyearDAL;
    
    public AcademicYearBLL()
    {
        academicyearDAL = new AcademicYearDAL();
    }
    
    public ArrayList<AcademicYearDTO> getAllAcademicYearToArray()
    {
        ArrayList<AcademicYearDTO> temp = new ArrayList<>();
        ResultSet rs = getAllAcademicYear();
        try {
            while(rs.next())
            {
                AcademicYearDTO state = new AcademicYearDTO(rs.getNString(1), rs.getNString(2));
                temp.add(state);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }
    /**
     * Ham lay tat ca cac Nam Hoc
     *
     * @return boolean
     */
    public ResultSet getAllAcademicYear() {
      
        return academicyearDAL.getAllAcademicYear();
    }
/**
     * Ham lay Nam Hoc theo ma Nam Hoc
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getAcademicYearById(String _id)
    {
      
        return academicyearDAL.getAcademicYearById(_id);
    }
      
    public String getMaxAcademicYear()
    {
        String temp = "";
        ResultSet rs = academicyearDAL.getMaxAcademicYear();
        try {
            while(rs.next())
            {
                temp = rs.getNString(1);
            }
        } catch (SQLException ex) {
           ex.printStackTrace();
        }
        return temp;
    }
     /**
     * ham them moi Nam Hoc xuong database
     * @param _academicyear
     * @return true: success, false: fail
     */
    public boolean insertAcademicYearToDatabase(AcademicYearDTO _academicyear)
    {
       return academicyearDAL.insertAcademicYearToDatabase(_academicyear);
    }
    
    /**
     * Ham cap nhat sua doi thong tin Nam Hoc
     * @param _academicyear
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateAcademicYearToDatabase(AcademicYearDTO _academicyear)
    {
        return academicyearDAL.updateAcademicYearToDatabase(_academicyear);
    }
    
    /**
     * Ham xoa mot Nam Hoc ra khoi database
     * @param _academicyear
     * @return 
     */
     public boolean deleteAcademicYearFromDatabase(AcademicYearDTO _academicyear)
    {
            
        return academicyearDAL.deleteAcademicYearFromDatabase(_academicyear);
    }
     
     public boolean checkInputIsInvalid(JXTable _jtb, String _input)
     {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", ""))){
                    return false;
                }
            }
        }
          return true;
     }
}
